package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.salary.entity.SalaryRental;
import com.boss.salary.service.IAgencyVerifyService;
import com.boss.salary.service.IAutoSubmitService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/AutoSubmitController")
public class AutoSubmitController  extends BasicinfoBaseController {
    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(AutoSubmitController.class);


    @Autowired
    private IAutoSubmitService autoSubmitService;
    @Autowired
    private IAgencyVerifyService iAgencyVerifyService;

    /**
     * 自动上报
     * @return
     */
    @PostMapping("/autoSubmit")
    public ApiResultDTO autoSubmit(@RequestHeader("X-Token") String tokenid,
                                   @RequestBody SalaryRental salaryRental) {
        StringBuffer result=new StringBuffer();
        try {

            UserDTO userDto = getUser(tokenid);
            //返回存在重复人员的信息
            List<Map<String, Object>> requestList= iAgencyVerifyService.selectOverlapPersionInfo(userDto,null);
            List<String> withOutAgencyIds=new ArrayList<>();
           if(!CollectionUtils.isEmpty(requestList)){
               for(Map<String, Object> map:requestList){
                   withOutAgencyIds.add(map.get("unsubagencyid").toString());
                   result.append("自动上报单位：")
                           .append(map.get("unsubagencycode"))
                           .append("-")
                           .append(map.get("unsubagencyname"))
                           .append(" (存在人员冲突)");
               }
           }else{

           }
             String subresult=autoSubmitService.autoReportSalary(userDto,withOutAgencyIds,salaryRental.getAgencyIds());
           if(StringUtils.isEmpty(subresult)) {
               result.append("自动上报成功");
               return ApiResultDTO.success(null,result.toString());
           }else{
               result.append("部分单位上报失败:</br>"+subresult);
               return ApiResultDTO.error(ERROR_RSCODE,result.toString());
           }


        } catch (Exception e) {
            logger.error("自动上报发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE,result.toString());
        }
    }
}
